/**
 * @author Ahamme
 * @date   2009 06
 * @update 2009 12  crop ROI
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package opgui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.ROIShape;
import javax.swing.ButtonGroup;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSpinner;
import javax.swing.JToolBar;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.InternationalFormatter;

import main.Board;
import main.IqmTools;
import main.Tank;


public class IqmCropGUI  extends IqmOperatorJFrame
						  implements IqmOperatorGUIBuilder, ActionListener, PropertyChangeListener, ChangeListener {

	private  ParameterBlockJAI   pbJAI         = null;  //  @jve:decl-index=0:
	//public  ImgProcFuncJFrame  opGUIFrame = null;
	//private  IqmCropGUI 		 opGUIFrame = null;  //actionPerformed events werden in dieser Klasse abgehandelt
	
	private  JRadioButtonMenuItem  buttManual         = null;	
	private  JRadioButtonMenuItem  buttROI            = null;	
	private  JRadioButtonMenuItem  buttMethodHidden   = null;
	private  JToolBar              jToolBarMethod     = null;
	private  ButtonGroup           buttGroupMethod    = null;
	
	private JPanel 				 	jPanelOldWidth				    = null;	
	private JLabel				 	jLabelOldWidth				    = null;
	private JFormattedTextField 	jFormattedTextFieldOldWidth     = null;	
	private JPanel 				 	jPanelOldHeight				    = null;	
	private JLabel				 	jLabelOldHeight				    = null;
	private JFormattedTextField 	jFormattedTextFieldOldHeight    = null;	
	private JPanel 				 	jPanelOffSetX		= null;	
	private JLabel				 	jLabelOffSetX		= null;
	private JSpinner 				jSpinnerOffSetX 	= null;
	private JPanel 				 	jPanelOffSetY		= null;	
	private JLabel				 	jLabelOffSetY		= null;
	private JSpinner			 	jSpinnerOffSetY 	= null;
	private JPanel 				 	jPanelNewWidth		= null;	
	private JLabel				 	jLabelNewWidth		= null;
	private JFormattedTextField 	jFormattedTextFieldNewWidth     = null;	
	private JSpinner			 	jSpinnerNewWidth 	= null;
	private JPanel 				 	jPanelNewHeight		= null;	
	private JLabel				 	jLabelNewHeight		= null;
	private JFormattedTextField 	jFormattedTextFieldNewHeight    = null;	
	private JSpinner			 	jSpinnerNewHeight 	= null;

	private  JRadioButtonMenuItem  buttCenter         = null;	
	private  JRadioButtonMenuItem  buttLeftTop        = null;	
	private  JRadioButtonMenuItem  buttLeftBottom     = null;	
	private  JRadioButtonMenuItem  buttRightTop       = null;	
	private  JRadioButtonMenuItem  buttRightBottom    = null;	
	private  JRadioButtonMenuItem  buttUser		      = null;		
	private  JRadioButtonMenuItem  buttOffSetHidden   = null;
	private  JToolBar              jToolBarOffSet     = null;
	private  ButtonGroup           buttGroupOffSet    = null;
	
	/**
	 * constructor
	 */
	public IqmCropGUI() {

	}

	
	/**
	 * This method sets all  variables to null
	 */
	private void resetVariables(){
		pbJAI         = null; 
		//opGUIFrame = null; 
		
		//jToolBarRGB  = null;

	}
	/**
	 * This method sets all parameters to null
	 */
	private void resetParameters(){
		//butt       = null;
		//butt       = null;
	}

	/**
	 * This method sets the current parameter block to the argument
	 * @param ParameterBlockJAI pbJAI
	 */
	@Override
	public void setParameterBlock(ParameterBlockJAI arg){
		pbJAI = arg;
		IqmTools.setCurrParameterBlockJAI(pbJAI);	
	}
	/**
	 * This method sets the current parameter block
	 * The individual values of the GUI  current ParameterBlock
	 *
	 */
	@Override
	public void setParameterBlock(){
		if (buttManual.isSelected())  pbJAI.setParameter("Method", 0);
		if (buttROI.isSelected())     pbJAI.setParameter("Method", 1);
		pbJAI.setParameter("OffSetX",   ((Number)jSpinnerOffSetX.getValue()).intValue());
		pbJAI.setParameter("OffSetY",   ((Number)jSpinnerOffSetY.getValue()).intValue());
		pbJAI.setParameter("NewWidth",  ((Number)jSpinnerNewWidth.getValue()).intValue());
		pbJAI.setParameter("NewHeight", ((Number)jSpinnerNewHeight.getValue()).intValue());
		if (buttCenter.isSelected())      pbJAI.setParameter("OffSet", 0);
		if (buttLeftTop.isSelected())     pbJAI.setParameter("OffSet", 1);
		if (buttLeftBottom.isSelected())  pbJAI.setParameter("OffSet", 2);
		if (buttRightTop.isSelected())    pbJAI.setParameter("OffSet", 3);
		if (buttRightBottom.isSelected()) pbJAI.setParameter("OffSet", 4);
		if (buttUser.isSelected())        pbJAI.setParameter("OffSet", 5);
		IqmTools.setCurrParameterBlockJAI(pbJAI);	
	}
	
	/**
	 * This method sets the current parameter values
	 *
	 */
	@Override
	public void setParameterValues(){
		int idx = IqmTools.getCurrManagerImgIndex();
	    PlanarImage pi = Tank.getCurrentTankImageAt(idx); 
	    if (pi == null){
	    	   System.out.println("IqmCropGUI: Current image not defined");
	    	   return;
	    }
		if (pbJAI.getIntParameter("Method") == 0) buttManual.setSelected(true);
		if (pbJAI.getIntParameter("Method") == 1) buttROI.setSelected(true);
       
	    jSpinnerOffSetX.removeChangeListener(this);	    	    
	    jSpinnerOffSetY.removeChangeListener(this);	    	    
	    jSpinnerNewWidth.removeChangeListener(this);	    	    
	    jSpinnerNewHeight.removeChangeListener(this);	    	    
	    jSpinnerOffSetX.setValue(pbJAI.getIntParameter("OffSetX"));
	    jSpinnerOffSetY.setValue(pbJAI.getIntParameter("OffSetY"));
	    jSpinnerNewWidth.setValue(pbJAI.getIntParameter("NewWidth"));
	    jSpinnerNewHeight.setValue(pbJAI.getIntParameter("NewHeight"));
	    jSpinnerOffSetX.addChangeListener(this);
	    jSpinnerOffSetY.addChangeListener(this);
	    jSpinnerNewWidth.addChangeListener(this);
	    jSpinnerNewHeight.addChangeListener(this);
	    
		if (pbJAI.getIntParameter("OffSet") == 0) buttCenter.setSelected(true);
		if (pbJAI.getIntParameter("OffSet") == 1) buttLeftTop.setSelected(true);
		if (pbJAI.getIntParameter("OffSet") == 2) buttLeftBottom.setSelected(true);
		if (pbJAI.getIntParameter("OffSet") == 3) buttRightTop.setSelected(true);
		if (pbJAI.getIntParameter("OffSet") == 4) buttRightBottom.setSelected(true);
		if (pbJAI.getIntParameter("OffSet") == 5) buttUser.setSelected(true);
		
		if ((buttUser.isSelected()) && (buttManual.isSelected())) {
			jSpinnerOffSetX.setEnabled(true);
			jSpinnerOffSetY.setEnabled(true);
		}else{
			jSpinnerOffSetX.setEnabled(false);
			jSpinnerOffSetY.setEnabled(false);
		}

	}

	/**
	 * This method creates and shows the GUI
	 */
	@Override
	public void createIqmOperatorGUI() {
		//System.out.println("IqmCrop: createAndShowGUI invoked");
		//opGUIFrame = new IqmCropGUI();      //actionPerforemd events werden in dieser Klasse abgehandelt
		//opGUIFrame.getJContentPane();
		this.initialize(); //Aufruf von ImgProcFuncJFrame
		this.setTitle("Crop");
		PlanarImage pi = Tank.getCurrentTankImageAt(IqmTools.getCurrManagerImgIndex());	
		
		//this.setSize(dim.width, dim.height+280);
		//this.setPreferredSize(new Dimension(800, 600));
		this.getJContentPane().add(getJToolBarMethod(),  getGridBagConstraintsButtonMethodGroup());
		this.getJContentPane().add(getJPanelOldWidth(),  getGridBagConstraintsOldWidth());
		this.getJContentPane().add(getJPanelOldHeight(), getGridBagConstraintsOldHeight());
		this.getJContentPane().add(getJPanelOffSetX(),   getGridBagConstraintsOffSetX());
		this.getJContentPane().add(getJPanelOffSetY(),   getGridBagConstraintsOffSetY());
		this.getJContentPane().add(getJPanelNewWidth(),  getGridBagConstraintsNewWidth());
		this.getJContentPane().add(getJPanelNewHeight(), getGridBagConstraintsNewHeight());
		this.getJContentPane().add(getJToolBarOffSet(),  getGridBagConstraintsButtonOffSetGroup());
		this.pack();
		//this.repaint();
	    jFormattedTextFieldOldWidth.setValue(pi.getWidth());
	    jFormattedTextFieldOldHeight.setValue(pi.getHeight());
		if (pbJAI == null) pbJAI = new ParameterBlockJAI("IqmCrop");  //nur wenn das erste mal aufgerufen
		//pbJAI = new ParameterBlockJAI("iqmthreshold"); //immer der default Block
		IqmTools.setCurrParameterBlockJAI(pbJAI);
		this.setParameterValues();
	
	}
	private  GridBagConstraints getGridBagConstraintsButtonMethodGroup(){
		GridBagConstraints gridBagConstraintsButtonMethodGroup = new GridBagConstraints();
		gridBagConstraintsButtonMethodGroup.gridx = 1;
		gridBagConstraintsButtonMethodGroup.gridy = 4;
		gridBagConstraintsButtonMethodGroup.gridwidth = 2;//?
		gridBagConstraintsButtonMethodGroup.insets = new Insets(20,0,20,0);  //top left bottom right		
		//gridBagConstraintsButtonMethodGroup.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsButtonMethodGroup;
	}
	
	private  GridBagConstraints getGridBagConstraintsOldWidth(){
		GridBagConstraints gridBagConstraintsOldWidth = new GridBagConstraints();
		gridBagConstraintsOldWidth.gridx = 1;
		gridBagConstraintsOldWidth.gridy = 5;
		gridBagConstraintsOldWidth.gridwidth = 1;//?
		gridBagConstraintsOldWidth.insets = new Insets(10,0,0,0); //top left bottom right
		//gridBagConstraintsOldWidth.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsOldWidth;
	}
	private  GridBagConstraints getGridBagConstraintsOldHeight(){
		GridBagConstraints gridBagConstraintsOldHeight = new GridBagConstraints();
		gridBagConstraintsOldHeight.gridx = 2;
		gridBagConstraintsOldHeight.gridy = 5;
		gridBagConstraintsOldHeight.gridwidth = 2;//?
		gridBagConstraintsOldHeight.insets = new Insets(10,20,0,0);  //top left bottom right
		//gridBagConstraintsOldHeight.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsOldHeight;
	}
	private  GridBagConstraints getGridBagConstraintsOffSetX(){
		GridBagConstraints gridBagConstraintsOffSetX = new GridBagConstraints();
		gridBagConstraintsOffSetX.gridx = 1;
		gridBagConstraintsOffSetX.gridy = 6;
		gridBagConstraintsOffSetX.gridwidth = 1;//?
		gridBagConstraintsOffSetX.insets = new Insets(30,0,0,0);  //top left bottom right
		//gridBagConstraintsOffSetX.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsOffSetX;
	}
	private  GridBagConstraints getGridBagConstraintsOffSetY(){
		GridBagConstraints gridBagConstraintsOffSetY = new GridBagConstraints();
		gridBagConstraintsOffSetY.gridx = 2;
		gridBagConstraintsOffSetY.gridy = 6;
		gridBagConstraintsOffSetY.gridwidth = 2;//?
		gridBagConstraintsOffSetY.insets = new Insets(30,20,0,0);  //top left bottom right
		//gridBagConstraintsOffSetY.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsOffSetY;
	}
	private  GridBagConstraints getGridBagConstraintsNewWidth(){
		GridBagConstraints gridBagConstraintsNewWidth = new GridBagConstraints();
		gridBagConstraintsNewWidth.gridx = 1;
		gridBagConstraintsNewWidth.gridy = 7;
		gridBagConstraintsNewWidth.gridwidth = 1;//?
		gridBagConstraintsNewWidth.insets = new Insets(10,0,0,0);  //top left bottom right
		//gridBagConstraintsNewWidth.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsNewWidth;
	}
	private  GridBagConstraints getGridBagConstraintsNewHeight(){
		GridBagConstraints gridBagConstraintsNewHeight = new GridBagConstraints();
		gridBagConstraintsNewHeight.gridx = 2;
		gridBagConstraintsNewHeight.gridy = 7;
		gridBagConstraintsNewHeight.gridwidth = 2;//?
		gridBagConstraintsNewHeight.insets = new Insets(10,20,0,0);  //top left bottom right
		//gridBagConstraintsNewHeight.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsNewHeight;
	}
	private  GridBagConstraints getGridBagConstraintsButtonOffSetGroup(){
		GridBagConstraints gridBagConstraintsButtonOffSetGroup = new GridBagConstraints();
		gridBagConstraintsButtonOffSetGroup.gridx = 2;
		gridBagConstraintsButtonOffSetGroup.gridy = 8;
		gridBagConstraintsButtonOffSetGroup.gridwidth = 2;//?
		gridBagConstraintsButtonOffSetGroup.insets = new Insets(10,0,20,0);  //top left bottom right		
		//gridBagConstraintsButtonOffSetGroup.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsButtonOffSetGroup;
	}
	//------------------------------------------------------------------------------------------------------------------
	/**
	 * This method closes and destroys the GUI
	 * 
	 * @return void
	 */
//	public void destroyGUI() {
//		if (this != null){
//					
//			//this.closeAndDestroyGUI();
//			//resetVariables();
//			//folgendes geht auch!?
//			this.setVisible(false);
//			this.dispose();
//			//this = null;
//		}
//	}
	/**
	 * This method updates the GUI
	 * This method overrides IqmOperatorJFrame
	 */
	@Override
	public void updateIqmOperatorGUI(){
		
	}
	//-----------------------------------------------------------------------
	/**
	 * This method initializes the Option: 
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtManual() {
		if (buttManual == null) {
			buttManual = new JRadioButtonMenuItem();
			buttManual.setText("Manual");
			buttManual.setToolTipText("manual crop settings");
			buttManual.addActionListener(this);
			buttManual.setActionCommand("parameter");
		}
		return buttManual;
	}
	/**
	 * This method initializes the Option: 
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtROI() {
		if (buttROI == null) {
			buttROI  = new JRadioButtonMenuItem();
			buttROI.setText("ROI");
			buttROI.setToolTipText("crop ROI");
			buttROI.addActionListener(this);
			buttROI.setActionCommand("parameter");
		}
		return buttROI;
	}
	/**
	 * This method initializes the hidden option	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtMethodHidden() {
		//if (buttMethodHidden == null) {
			buttMethodHidden  = new JRadioButtonMenuItem();
			buttMethodHidden .setText("Hidden");
			buttMethodHidden.setVisible(false);
		//}
		return buttMethodHidden;
	}
	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private   JToolBar getJToolBarMethod() {
//		if (jToolBarMethod== null) {
			jToolBarMethod = new JToolBar("Options");
			jToolBarMethod.setOrientation(0); // 0 Horizontal 1 Vertical orientation
			jToolBarMethod.add(getJRadioButtonMenuButtManual());
			jToolBarMethod.add(getJRadioButtonMenuButtROI());
			//jToolBarMethod.addSeparator();
			this.setButtonGroupMethod();	//Gruppieren der JRadioButtons
//		}
		return jToolBarMethod;
	}
	private  void setButtonGroupMethod() {
		//if (ButtonGroup buttGroup == null) {
			buttGroupMethod = new ButtonGroup();
			buttGroupMethod.add(buttManual);
			buttGroupMethod.add(buttROI);
			buttGroupMethod.add(buttMethodHidden);
	}

	

	//-----------------------------------------------------------------------
//	class IntNumberVerifier extends InputVerifier { //damit muss Eingabe richtig sein 
//		@Override
//		public boolean verify(JComponent input) {
//		   JFormattedTextField ftf = (JFormattedTextField)input;
//		   JFormattedTextField.AbstractFormatter formatter = ftf.getFormatter();
//		   if (formatter != null) {
//		      String text = ftf.getText();
//		  	  try {
//				   text = text.replace(",", ".");
//				   Integer.valueOf(text);
//				   //Float.valueOf(text);
//					return true;
//			  } catch (NumberFormatException e) {
//					return false;
//			  }	 
//		   }
//		   return true;
//		}
////		public boolean shouldYieldFocus(JComponent input) {
////		System.out.println("NumberVerifier  shouldYieldFocus");
////
////          return verify(input);
////    }
//	}
	/**
	 * This method initializes jJPanelOldWidth	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelOldWidth() {
		if (jPanelOldWidth == null) {
			jPanelOldWidth = new JPanel();
			jPanelOldWidth.setLayout(new BorderLayout());
			//jPanelOldWidth.setPreferredSize(new Dimension(250,18));					
			jLabelOldWidth = new JLabel("Old Width: ");
			jLabelOldWidth.setPreferredSize(new Dimension(65,18));
			jLabelOldWidth.setHorizontalAlignment(JLabel.RIGHT);
			jFormattedTextFieldOldWidth = new JFormattedTextField(NumberFormat.getNumberInstance());
			jFormattedTextFieldOldWidth.setPreferredSize(new Dimension(70,18));
			//jFormattedTextFieldOldWidth.addPropertyChangeListener("value", this);
			//jFormattedTextFieldOldWidth.setInputVerifier(new IntNumberVerifier());
			jFormattedTextFieldOldWidth.setEditable(false);
			//jFormattedTextFieldOldWidth.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);	
			jFormattedTextFieldOldWidth.setColumns(5);
			InternationalFormatter intFormatter = (InternationalFormatter)jFormattedTextFieldOldWidth.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;
			jPanelOldWidth.add(jLabelOldWidth, 			  BorderLayout.WEST);
			jPanelOldWidth.add(jFormattedTextFieldOldWidth, BorderLayout.CENTER);	
		
		}
		return jPanelOldWidth;
	}
	/**
	 * This method initializes jJPanelOldHeight	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelOldHeight() {
		if (jPanelOldHeight == null) {
			jPanelOldHeight = new JPanel();
			jPanelOldHeight.setLayout(new BorderLayout());			
			jLabelOldHeight = new JLabel("Old Height: ");
			jLabelOldHeight.setPreferredSize(new Dimension(65,18));
			jLabelOldHeight.setHorizontalAlignment(JLabel.RIGHT);
			jFormattedTextFieldOldHeight = new JFormattedTextField(NumberFormat.getNumberInstance());
			jFormattedTextFieldOldHeight.setPreferredSize(new Dimension(70,18));
			//jFormattedTextFieldOldHeight.addPropertyChangeListener("value", this);
			//jFormattedTextFieldOldHeight.setInputVerifier(new IntNumberVerifier());
			jFormattedTextFieldOldHeight.setEditable(false);
			//jFormattedTextFieldOldHeight.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);	
			jFormattedTextFieldOldHeight.setColumns(5);
			InternationalFormatter intFormatter = (InternationalFormatter)jFormattedTextFieldOldHeight.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;
			jPanelOldHeight.add(jLabelOldHeight, 			  BorderLayout.WEST);
			jPanelOldHeight.add(jFormattedTextFieldOldHeight, BorderLayout.CENTER);
			jFormattedTextFieldOldHeight.setEnabled(true);	
		}
		return jPanelOldHeight;
	}
	/**
	 * This method initializes jJPanelOffSetX	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelOffSetX() {
		if (jPanelOffSetX == null) {
			jPanelOffSetX = new JPanel();
			jPanelOffSetX.setLayout(new BorderLayout());		
			jLabelOffSetX = new JLabel("OffSet X: ");
			jLabelOffSetX.setPreferredSize(new Dimension(65, 22));
			jLabelOffSetX.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(0, 0, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerOffSetX = new JSpinner(sModel);
			jSpinnerOffSetX.setPreferredSize(new Dimension(60, 22));
			jSpinnerOffSetX.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerOffSetX.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelOffSetX.add(jLabelOffSetX,   BorderLayout.WEST);
			jPanelOffSetX.add(jSpinnerOffSetX, BorderLayout.CENTER);			
		}
		return jPanelOffSetX;
	}
	/**
	 * This method initializes jJPanelOffSetY	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelOffSetY() {
		if (jPanelOffSetY == null) {
			jPanelOffSetY = new JPanel();
			jPanelOffSetY.setLayout(new BorderLayout());		
			jLabelOffSetY = new JLabel("OffSet Y: ");
			jLabelOffSetY.setPreferredSize(new Dimension(65, 22));
			jLabelOffSetY.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(0, 0, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerOffSetY = new JSpinner(sModel);
			jSpinnerOffSetY.setPreferredSize(new Dimension(60, 22));
			jSpinnerOffSetY.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerOffSetY.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelOffSetY.add(jLabelOffSetY,   BorderLayout.WEST);
			jPanelOffSetY.add(jSpinnerOffSetY, BorderLayout.CENTER);			
		}
		return jPanelOffSetY;
	}
	/**
	 * This method initializes jJPanelNewWidth	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelNewWidth() {
		if (jPanelNewWidth == null) {
			jPanelNewWidth = new JPanel();
			jPanelNewWidth.setLayout(new BorderLayout());		
			jLabelNewWidth = new JLabel("New Width: ");
			jLabelNewWidth.setPreferredSize(new Dimension(70, 22));
			jLabelNewWidth.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(0, 0, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerNewWidth = new JSpinner(sModel);
			jSpinnerNewWidth.setPreferredSize(new Dimension(60, 22));
			jSpinnerNewWidth.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerNewWidth.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			jFormattedTextFieldNewWidth = ftf;
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelNewWidth.add(jLabelNewWidth, 	 BorderLayout.WEST);
			jPanelNewWidth.add(jSpinnerNewWidth, BorderLayout.CENTER);			
		}
		return jPanelNewWidth;
	}
	/**
	 * This method initializes jJPanelNewHeight	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelNewHeight() {
		if (jPanelNewHeight == null) {
			jPanelNewHeight = new JPanel();
			jPanelNewHeight.setLayout(new BorderLayout());		
			jLabelNewHeight = new JLabel("New Height: ");
			jLabelNewHeight.setHorizontalAlignment(JLabel.RIGHT);
			jLabelNewHeight.setPreferredSize(new Dimension(70, 22));
			SpinnerModel sModel = new SpinnerNumberModel(0, 0, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerNewHeight = new JSpinner(sModel);
			jSpinnerNewHeight.setPreferredSize(new Dimension(60, 22));
			jSpinnerNewHeight.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerNewHeight.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			jFormattedTextFieldNewHeight = ftf;
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelNewHeight.add(jLabelNewHeight, 	 BorderLayout.WEST);
			jPanelNewHeight.add(jSpinnerNewHeight, BorderLayout.CENTER);			
		}
		return jPanelNewHeight;
	}
	

	//--------------------------------------------------------------------------------------------
	/**
	 * This method initializes the Option: 
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtCenter() {
		if (buttCenter == null) {
			buttCenter = new JRadioButtonMenuItem();
			buttCenter.setText("Center");
			buttCenter.setToolTipText("crop center");
			buttCenter.addActionListener(this);
			buttCenter.setActionCommand("parameter");
		}
		return buttCenter;
	}
	/**
	 * This method initializes the Option: 
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtLeftTop() {
		if (buttLeftTop == null) {
			buttLeftTop = new JRadioButtonMenuItem();
			buttLeftTop.setText("Left Top");
			buttLeftTop.setToolTipText("crop left top");
			buttLeftTop.addActionListener(this);
			buttLeftTop.setActionCommand("parameter");
		}
		return buttLeftTop;
	}
	/**
	 * This method initializes the Option: 
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtLeftBottom() {
		//if (buttLeftBottom == null) {
			buttLeftBottom  = new JRadioButtonMenuItem();
			buttLeftBottom.setText("Left Bottom");
			buttLeftBottom.setToolTipText("crop left bottom");
			buttLeftBottom.addActionListener(this);
			buttLeftBottom.setActionCommand("parameter");
		//}
		return buttLeftBottom;
	}
	/**
	 * This method initializes the Option: 
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtRightTop() {
		//if (buttRightTop == null) {
			buttRightTop  = new JRadioButtonMenuItem();
			buttRightTop.setText("Right Top");
			buttRightTop.setToolTipText("crop right top");
			buttRightTop.addActionListener(this);
			buttRightTop.setActionCommand("parameter");
		//}
		return buttRightTop;
	}
	/**
	 * This method initializes the Option: 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtRightBottom() {
		//if (buttRightBottom == null) {
			buttRightBottom = new JRadioButtonMenuItem();
			buttRightBottom.setText("Right Bottom");
			buttRightBottom.setToolTipText("crop right bottom");
			buttRightBottom.addActionListener(this);
			buttRightBottom.setActionCommand("parameter");
		//}
		return buttRightBottom;
	}
	/**
	 * This method initializes the Option: 
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtUser() {
		//if (buttUser == null) {
			buttUser = new JRadioButtonMenuItem();
			buttUser.setText("User");
			buttUser.setToolTipText("Offset user values");
			buttUser.addActionListener(this);
			buttUser.setActionCommand("parameter");
		//}
		return buttUser;
	}
	/**
	 * This method initializes the hidden option	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtOffSetHidden() {
		//if (buttOffSetHidden == null) {
			buttOffSetHidden = new JRadioButtonMenuItem();
			buttOffSetHidden.setText("Hidden");
			buttOffSetHidden.setVisible(false);
		//}
		return buttOffSetHidden;
	}
	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private   JToolBar getJToolBarOffSet() {
//		if (jToolBarOffSet== null) {
			jToolBarOffSet = new JToolBar("Options");
			jToolBarOffSet.setOrientation(1); // 0 Horizontal 1 Vertical orientation
			jToolBarOffSet.add(getJRadioButtonMenuButtCenter());
			jToolBarOffSet.add(getJRadioButtonMenuButtLeftTop());
			jToolBarOffSet.add(getJRadioButtonMenuButtLeftBottom());
			jToolBarOffSet.add(getJRadioButtonMenuButtRightTop());
			jToolBarOffSet.add(getJRadioButtonMenuButtRightBottom());
			jToolBarOffSet.add(getJRadioButtonMenuButtUser());
			jToolBarOffSet.add(getJRadioButtonMenuButtOffSetHidden());
			//jToolBarOffSet.addSeparator();
			this.setButtonGroupOffSet();	//Gruppieren der JRadioButtons
//		}
		return jToolBarOffSet;
	}
	private  void setButtonGroupOffSet() {
		//if (ButtonGroup buttGroup == null) {
			buttGroupOffSet = new ButtonGroup();
			buttGroupOffSet.add(buttCenter);
			buttGroupOffSet.add(buttLeftTop);
			buttGroupOffSet.add(buttLeftBottom);
			buttGroupOffSet.add(buttRightTop);
			buttGroupOffSet.add(buttRightBottom);
			buttGroupOffSet.add(buttUser);
			buttGroupOffSet.add(buttOffSetHidden);
	}

	//--------------------------------------------------------------------------------------------
	public void actionPerformed(ActionEvent e) {
		//System.out.println("IqmCropGUI: event e: " +e.getActionCommand());
		if ("reset".equals(e.getActionCommand())) {
			ParameterBlockJAI pbJAI = new ParameterBlockJAI("IqmCrop".toLowerCase());
			IqmTools.setCurrParameterBlockJAI(pbJAI);
			this.setParameterBlock(pbJAI);
			this.setParameterValues();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
			this.reset();
		}
		if ("parameter".equals(e.getActionCommand())) {

			if (buttManual == e.getSource()){
				//jPanelOffSetX.setEnabled(true);
				//jPanelOffSetY.setEnabled(true);
				jSpinnerOffSetX.setEnabled(true);
				jSpinnerOffSetY.setEnabled(true);
				jSpinnerNewWidth.setEnabled(true);
				jSpinnerNewHeight.setEnabled(true);
				jFormattedTextFieldNewWidth.setEditable(true);
				jFormattedTextFieldNewHeight.setEditable(true);
				//jFormattedTextFieldNewWidth.setEnabled(true);
				//jFormattedTextFieldNewHeight.setEnabled(true);
			}
			if (buttROI == e.getSource()){
				ROIShape rs = IqmTools.getLookDisplayJAI().getCurrROIShape();
				if (rs == null){
					Board.appendTexln("IqmCropGUI: No ROI defined!");
					return;
				}
				//jPanelOffSetX.setEnabled(false);
				//jPanelOffSetY.setEnabled(false);
				jSpinnerOffSetX.setEnabled(false);
				jSpinnerOffSetY.setEnabled(false);
				jSpinnerNewWidth.setEnabled(false);
				jSpinnerNewHeight.setEnabled(false);
				jFormattedTextFieldNewWidth.setEditable(false);
				jFormattedTextFieldNewHeight.setEditable(false);
				//jFormattedTextFieldNewWidth.setEnabled(false);
				//jFormattedTextFieldNewHeight.setEnabled(false);
				
				int idx = IqmTools.getCurrManagerImgIndex();
			    PlanarImage pi = Tank.getCurrentTankImageAt(idx); 
			    int height = pi.getHeight();
				Rectangle bounds = rs.getBounds();
				jSpinnerOffSetX.setValue(bounds.x);
				jSpinnerOffSetY.setValue(height - bounds.y - bounds.height);				
				jFormattedTextFieldNewWidth.setValue(bounds.width);
				jFormattedTextFieldNewHeight.setValue(bounds.height);
			}
//			if (buttUser == e.getSource()) {
//				jSpinnerOffSetX.setEnabled(true);
//				jSpinnerOffSetY.setEnabled(true);
//			}else{
//				jSpinnerOffSetX.setEnabled(false);
//				jSpinnerOffSetY.setEnabled(false);
//			}
			if ((buttUser.isSelected()) && (buttManual.isSelected())) {
				jSpinnerOffSetX.setEnabled(true);
				jSpinnerOffSetY.setEnabled(true);
			}else{
				jSpinnerOffSetX.setEnabled(false);
				jSpinnerOffSetY.setEnabled(false);
			}
			this.setParameterBlock();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		}
	
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}

	}
	//-----------------------------------------------------------------------------------------------

	@Override
	public void propertyChange(PropertyChangeEvent e) {
		int offSetX   = ((Number)jSpinnerOffSetX.getValue()).intValue();
		int offSetY   = ((Number)jSpinnerOffSetY.getValue()).intValue();
		int newWidth  = ((Number)jSpinnerNewWidth.getValue()).intValue();
		int newHeight = ((Number)jSpinnerNewHeight.getValue()).intValue();
		int oldWidth  = ((Number)jFormattedTextFieldOldWidth.getValue()).intValue();
		int oldHeight = ((Number)jFormattedTextFieldOldHeight.getValue()).intValue();
		
		if (jFormattedTextFieldOldWidth == e.getSource()) {
		}
		if (jFormattedTextFieldOldHeight  == e.getSource()) {			
		}
		this.setParameterBlock();
		//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}
	}
	//-------------------------------------------------------------------------------------------------
	@Override
	public void stateChanged(ChangeEvent e) {
		
		int offSetX   = ((Number)jSpinnerOffSetX.getValue()).intValue();
		int offSetY   = ((Number)jSpinnerOffSetY.getValue()).intValue();
		int newWidth  = ((Number)jSpinnerNewWidth.getValue()).intValue();
		int newHeight = ((Number)jSpinnerNewHeight.getValue()).intValue();
		int oldWidth  = ((Number)jFormattedTextFieldOldWidth.getValue()).intValue();
		int oldHeight = ((Number)jFormattedTextFieldOldHeight.getValue()).intValue();
		

		if (jSpinnerOffSetX  == e.getSource()) {
			if(offSetX + newWidth > oldWidth) jSpinnerNewWidth.setValue(oldWidth - offSetX );
		}
		if (jSpinnerOffSetY  == e.getSource()) {
			if(offSetY + newHeight > oldHeight) jSpinnerNewHeight.setValue(oldHeight - offSetY);
		}
		if (jSpinnerNewWidth  == e.getSource()) {
			if(newWidth > oldWidth){
				jSpinnerNewWidth.setValue(oldWidth);
				newWidth = oldWidth;
			}
			if(offSetX + newWidth > oldWidth) jSpinnerOffSetX.setValue(oldWidth - newWidth);
		}
		if (jSpinnerNewHeight  == e.getSource()) {
			if(newHeight > oldHeight){
				jSpinnerNewHeight.setValue(oldHeight);
				newHeight = oldHeight;
			}
			if(offSetY + newHeight > oldHeight) jSpinnerOffSetY.setValue(oldHeight - newHeight);
		}
		this.setParameterBlock();
		//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}
		
	}


	//-------------------------------------------------------------------------------------------------
	public static void main() {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				IqmCropGUI gui = new IqmCropGUI();
				gui.createIqmOperatorGUI();

			}
		});
	}
	
}//END
